//package com.lshlmy.gateway.config;
//
//import com.lshlmy.common.web.constant.SecurityConstants;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.web.server.ServerWebExchange;
//import org.springframework.web.server.WebFilter;
//import org.springframework.web.server.WebFilterChain;
//import reactor.core.publisher.Mono;
//
//import java.io.UnsupportedEncodingException;
//import java.net.URI;
//import java.net.URLEncoder;
//
///**
// * @author liangxiangming
// * @date 2022/09/19
// **/
//@Configuration
//public class LoginConfig {
//
//    @Bean
//    public WebFilter getSsoLoginFilter() {
//        return new WebFilter() {
//
//            @Override
//            public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
//                try {
//                    ServerHttpRequest request = exchange.getRequest();
//                    String path = request.getPath().toString();
//                    if (SecurityConstants.OAUTH_TOKEN_URL.equals(path)) {
//                        String loginToken = request.getQueryParams().getFirst(LoginTokenUtil.LOGIN_CALLBACK_TOKEN);
//                        String loginUrl = request.getQueryParams().getFirst(LoginTokenUtil.LOGIN_CALLBACK_URL);
//                        if (StringUtils.isAnyBlank(loginToken, loginUrl)) {
//                            throw new RuntimeException("参数错误!");
//                        }
//                        String url = URLDecoder.decode(loginUrl, "UTF-8");
//                        boolean validateLoginToken = LoginTokenUtil.validateLoginToken(loginToken);
//                        if (validateLoginToken) {
//                            ServerHttpResponse response = exchange.getResponse();
//                            response.setStatusCode(HttpStatus.SEE_OTHER);// 校验成功，重定向
//                            response.getHeaders().set(HttpHeaders.LOCATION, url);
//                            return exchange.getResponse().setComplete();
//                        }
//                        // 校验失败，抛异常或者重新执行登录
//                        // redirectSSO(exchange, request, url);
//                        throw new RuntimeException("token校验失败!");
//                    }
//                    String loginToken = LoginTokenUtil.getLoginToken(exchange);
//                    Mono<Void> result = null;
//                    if (StringUtils.isNoneBlank(loginToken)) {
//                        boolean hasLogin = LoginTokenUtil.validateLoginToken(loginToken);
//                        if (!hasLogin) {
//                            result = redirectSSO(exchange, request, path);
//                        } else {
//                            return chain.filter(exchange);
//                        }
//                    } else {
//                        result = redirectSSO(exchange, request, path);
//                    }
//                    if (null != result) {
//                        return result;
//                    }
//                    throw new RuntimeException("token校验失败!");
//                } catch (Exception e) {
//                    exchange.getResponse().getHeaders().set("Content-Type", "application/json; charset=utf-8");
//                    return exchange.getResponse().writeWith(Mono.just(exchange.getResponse().bufferFactory().wrap(e.getMessage().getBytes())));
//                }
//            }
//
//            /**
//             * 调单点登录
//             * @param exchange 上下文
//             * @param request 请求
//             * @param path 请求地址
//             * @return
//             */
//            private Mono<Void> redirectSSO(ServerWebExchange exchange, ServerHttpRequest request, String path) {
//                Mono<Void> result = match(pageUrlsConfig.getUrls(), ignoreUrlsConfig.getUrls(), path, () -> {
//                    if (isPage(path)) {
//                        URI uri = request.getURI();
//                        String url = "/";
//                        try {
//                            url = LoginTokenUtil.LOGIN_PATH // 登录服务地址
//                                    + URLEncoder.encode(uri.getScheme() + "://" + uri.getAuthority() // gateway服务（http://gateway）
//                                    + LoginTokenUtil.LOGIN_CALLBACK_PATH + "?" + LoginTokenUtil.LOGIN_CALLBACK_URL + "=" // gateway回调地址参数 http://gateway/sso/login?url=
//                                    + URLEncoder.encode(uri.toString(), "UTF-8"), "UTF-8");// 登录成功重定向地址
//                        } catch (UnsupportedEncodingException e1) {
//                            e1.printStackTrace();
//                        }
//                        ServerHttpResponse response = exchange.getResponse();
//                        response.setStatusCode(HttpStatus.SEE_OTHER);
//                        response.getHeaders().set(HttpHeaders.LOCATION, url);
//                        exchange.getResponse().getHeaders().set("Content-Type", "text/plain; charset=utf-8");
//                        return exchange.getResponse().setComplete();
//                    }
//                    return null;
//                });
//                return result;
//            }
//
//        };
//    }
//}